﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace InventoryMIS2019
{
    public partial class _Product : InventoryMIS2019.Templates.BaselnfoForm
    {
        private ProductBLL bl;
        private Product product;
        private List<Product> list;
       
        public _Product()
        {
            InitializeComponent();
            bl = new ProductBLL();
            list = new List<Product>();
          
            BindDataGridView();
        }
        private void ControlOpera(string opera)
        {
            
            texprProductName.Enabled =
            texprSpellingCode.Enabled =
            texprUnit.Enabled =
            texprOrigin.Enabled =
            texprPurchasePrice.Enabled =
            texprQuantity.Enabled = true;
            if (opera =="U"||opera =="D")
            {
                texprProductID.Enabled = false;
            }
            else
            {
                texprProductID.Enabled = true;
            }
        }
        private void BindDataGridView()
        {
            dataProduct.DataSource = null;//清空当前表格中的数据
            list = bl.GetList();
          
          
            dataProduct.DataSource = list;//将查询结果绑定至控件dgvCategory
        }
        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void TemNew_Click(object sender, EventArgs e)
        {
            ControlOpera("I");
        }

        private void TemModify_Click(object sender, EventArgs e)
        {
            ControlOpera("U");
        }

        private void TemDelete_Click(object sender, EventArgs e)
        {
            ControlOpera("D");
        }
        private Product SetValue()
        {
            product = new Product();
            product.ProductID = texprProductID.Text.Trim();
            product.ProductName = texprProductName.Text.Trim();
            product.SpellingCode = texprSpellingCode.Text.Trim();
            product.BarCode = texprBarCode.Text.Trim();
            product.Special = texprSpecial.Text.Trim();
            product.Unit = texprUnit.Text.Trim();
            product.Origin = texprOrigin.Text.Trim();
            product.PurchasePrice =decimal .Parse ( texprPurchasePrice.Text.Trim());
            product.SalePrice = decimal.Parse(texprSalePrice.Text.Trim());
            product.Quantity = int.Parse(texprQuantity .Text .Trim ());

            product.Category.CategoryID = comprCategoryID.SelectedValue.ToString();
                return product;
        }

        private void _Product_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void texprUnit_TextChanged(object sender, EventArgs e)
        {

        }

        private void comprCategoryID_VisibleChanged(object sender, EventArgs e)
        {
            DataSet cList = bl.CategoryList();
            comprCategoryID.DataSource = cList.Tables[0];
            comprCategoryID.DisplayMember = "CategoryName";
            comprCategoryID.ValueMember = "CategoryID";
        }
        private string Validation()
        {
            if (texprProductID .Text .Trim ().Length ==0)
            {
                texprProductID.Focus();
                return "产品号不能为空";
            }
            if (texprProductName.Text.Trim().Length == 0)
            {
                texprProductName.Focus();
                return "产品名称不能为空";
            }
            if (texprSpecial.Text.Trim().Length == 0)
            {
                texprSpecial.Focus();
                return "规格不能为空";
            }
            if (texprUnit.Text.Trim().Length == 0)
            {
                texprUnit.Focus();
                return "计量单位不能为空";
            }
            if (texprOrigin.Text.Trim().Length == 0)
            {
                texprOrigin.Focus();
                return "产地不能为空";
            }
            if (comprCategoryID.Text.Trim().Length == 0)
            {
                comprCategoryID.Focus();
                return "产品类别不能为空";
            }
            if (texprPurchasePrice.Text.Trim().Length == 0)
            {
                texprPurchasePrice.Focus();
                return "购入价不能为空";
            }
            if (texprQuantity.Text.Trim().Length == 0)
            {
                texprQuantity.Focus();
                return "数量不能为空";
            }
            return "";
        }
        private void ShowInfo()
        {
            texprProductID.Text = product.ProductID .Trim();
            texprProductName.Text = product.ProductName .Trim();
            texprSpellingCode.Text = product.SpellingCode .Trim();
            texprBarCode.Text = product.BarCode .Trim();
            texprSpecial.Text = product.Special .Trim();
            texprUnit.Text = product.Unit .Trim();
            texprOrigin.Text = product.Origin .Trim();
            comprCategoryID.Text = product.Category .CategoryID .Trim();
            texprPurchasePrice.Text = product.PurchasePrice .ToString ();
            texprSalePrice.Text = product.SalePrice .ToString ();
            texprQuantity.Text = product.Quantity .ToString ();
            comprCategoryID.Text = product.Category.CategoryName;
        }
        private void BindData()
        {
            dataProduct.DataSource = null;//清空DataGridView的数据源
            list = bl.GetList();
            dataProduct.DataSource = list;//将list集合数据绑定到控件dgvCustomer作为数据源
        }
        private void TemInventory_Click(object sender, EventArgs e)
        {
            string errorString = Validation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示");return; }
            if (opera ==Operation.INSERT )
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Insert(product ))
                {
                    BindData();//绑定DataGridView的数据源                    
                    Clear();//清空TextBox控件内容
                    MessageBox.Show("新增数据成功！", "系统提示");
                }
                else
                    MessageBox.Show("新增数据失败！", "系统提示");

            }
            if (product != null)
            {
                SetValue();//获取控件值并完成对象赋值
                if (bl.Update(product))
                {
                    BindData();//绑定DataGridView的数据源
                    ShowInfo();//绑定回控件

                    MessageBox.Show("成功更新数据！", "系统提示");
                }
                else
                    MessageBox.Show("更新数据失败！", "系统提示");
            }
            else
                MessageBox.Show("请选择需要修改的记录", "系统提示");
            if (opera == Operation.UPDATE)
            {
                if (product != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Update(product))
                    {
                        BindData();//绑定DataGridView的数据源
                        ShowInfo();

                        MessageBox.Show("成功更新数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("更新数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要修改的记录", "系统提示");
            }
            if (opera == Operation.DELETE)
            {
                if (product != null)
                {
                    SetValue();//获取控件值并完成对象赋值
                    if (bl.Delete(product))
                    {
                        BindData();//绑定DataGridView的数据源

                        Clear();//清空TextBox控件内容
                        MessageBox.Show("成功删除数据！", "系统提示");
                    }
                    else
                        MessageBox.Show("删除数据失败！", "系统提示");
                }
                else
                    MessageBox.Show("请选择需要删除的记录", "系统提示");
            }
        }

        private void texprSpellingCode_TextChanged(object sender, EventArgs e)
        {

        }
        //根据产品名称自动生成拼音码
        private void texprProductName_TextChanged(object sender, EventArgs e)
        {
            texprSpellingCode.Text = PY.HZToSpell(texprProductName.Text.Trim());
        }
        //生成条形码方法
        //注意：此条形码生成并不符合条形码生成规范
        private string CreateBarCode()
        {
            //Ticks--当前实例日期和时间周期数
            //tofiletimeutc ---讲datetime值转算成Windows文件时间
            //gethashcode--
            string val = DateTime.Now.GetHashCode().ToString() + System.Environment.TickCount;
            val = val.Substring(val.Length - 13);
            return val;
        }

        private void texprBarCode_VisibleChanged(object sender, EventArgs e)
        {
            texprBarCode.Text = CreateBarCode();
        }

        private void texprSalePrice_TextChanged(object sender, EventArgs e)
        {

        }
        //根据购入价格增加50%来取消售价
        private void texprPurchasePrice_TextChanged(object sender, EventArgs e)
        {
            if (texprPurchasePrice.Text .Length >0)
            {
                texprSalePrice.Text = (double.Parse(texprPurchasePrice.Text.Trim()) * 1.5).ToString ();
            }
        }

        private void dataProduct_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (dataProduct.Columns[e.ColumnIndex].Name == "CategoryName")
                e.Value = ((Category )e.Value).CategoryName ;        
        }

        private void comprCategoryID_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
        //1：缺少下拉菜单选择后，产品类型ID值的获取方法实现
        //   事件对应下拉控件SelectedIndexChanged事件
    }
}
